package com.reids.ch01.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.text.SimpleDateFormat;

/***
 *
 * jackson 配置
 * @author wind
 * @since 2020-06-22
 */
@Configuration
public class JacksonConfig {

    @Bean
//    @Primary
//    @ConditionalOnMissingBean
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        // null值过滤，只对vo起作用 ，map，list不起作用
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        // Date类型 返回格式long转为 "yyyy-MM-dd HH:mm:ss"
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        objectMapper.setDateFormat(dateFormat);
        return objectMapper;
    }
}
